package com.gm.service.user;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.gm.common.Pager;
import com.gm.controller.dto.CardDTO;
import com.gm.dao.card.ICardDao;
import com.gm.model.po.Card;
import com.gm.tools.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/6/26.
 */
@Service
public class CardService {

    @Autowired
    private ICardDao cardDao;


    public PageList<CardDTO> getCardList(Pager pager, Integer cid){

        PageBounds pageBounds = new PageBounds(pager.getCurrentPage(), pager.getPageSize());
        return cardDao.getCardList(pageBounds,cid);
    }

    public List<Card> getAllCardByNow(Integer nowDate){
        return cardDao.getAllCardByNow(nowDate);
    }

    public int addCard(Card card){
        card.setStatus(1);
        card.setCreateTime(DateUtils.getCurrentUnixTime());
        card.setUpdateTime(DateUtils.getCurrentUnixTime());
        return cardDao.insert(card);
    }

    public int updateCard(Card record){
        Card  card = cardDao.selectByPrimaryKey(record.getId());
        card.setCtid(record.getCtid());
        card.setBalance(record.getBalance());
        card.setLimit(record.getLimit());
        card.setRelLimit(record.getRelLimit());
//        card.setNum(record.getNum());
        card.setPwd(record.getPwd());
        card.setCtmRepaymentDate(record.getCtmRepaymentDate());
        card.setCtmStatementDate(record.getCtmStatementDate());
        card.setStmRepaymentDate(record.getStmRepaymentDate());
        card.setStmStatementDate(record.getStmStatementDate());
        card.setRemark(record.getRemark());
        card.setStatus(record.getStatus());
        card.setUpdateTime(DateUtils.getCurrentUnixTime());
        return cardDao.updateByPrimaryKey(card);
    }

    public int deleteCard(Integer id){
        Card card = cardDao.selectByPrimaryKey(id);
        card.setStatus(-1);
        return cardDao.updateByPrimaryKey(card);
    }

    public Card selectByPrimaryKey(Integer id){
        return cardDao.selectByPrimaryKey(id);
    }

    public List<Card> selectByNum(String num){
        return cardDao.selectByNum(num);
    }
}
